package heterogeneousContainers;

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;

/**
 * Created with IntelliJ IDEA.
 * User: Georgiana.Scarlat
 * Date: 8/30/12
 * Time: 3:36 PM
 *
 */
@Deprecated
public class AnnotationExample {

    public static final String DEPRECATED = "java.lang.Deprecated";

    public static void main(String[] args) throws ClassNotFoundException {

        AnnotatedElement elem = AnnotationExample.class;
        Annotation annotation = getSomeAnnotationUnchecked(elem, DEPRECATED);

        System.out.println(annotation);

        annotation = getSomeAnnotation(elem,DEPRECATED );

        System.out.println(annotation);
    }



    private static Annotation getSomeAnnotationUnchecked(AnnotatedElement elem, String name) throws ClassNotFoundException {

        Class<?> annotationClass = Class.forName(name);

        return elem.getAnnotation((Class<? extends Annotation>)annotationClass);
    }

    private static Annotation getSomeAnnotation(AnnotatedElement elem, String name) throws ClassNotFoundException {

        Class<?> annotationClass = Class.forName(name);

        return elem.getAnnotation(annotationClass.asSubclass(Annotation.class));
    }

}
